package ar.edu.unlp.info.cptn.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import ar.edu.unlp.info.cptn.dao.PersonaDao;
import ar.edu.unlp.info.cptn.model.Asignatura;
import ar.edu.unlp.info.cptn.model.Persona;

@Repository
public class PersonaDaoImpl  extends HibernateDaoSupport implements PersonaDao{
	
	@Autowired
	public PersonaDaoImpl(SessionFactory factory){
		this.setSessionFactory(factory);
	}

	public void guardar(Persona persona) {
		this.getHibernateTemplate().saveOrUpdate(persona);
	}

	public Persona buscar(String dni) {
		return null;
	}

	@SuppressWarnings("unchecked")
	public List getAll(Class clazz) {
		return this.getHibernateTemplate().loadAll(clazz);
	}

	@Override
	public Object get(Class clazz, Long id) {
		return this.getHibernateTemplate().get(clazz, id);
	}

	@Override
	public Persona login(String usuario, String password) {
		Persona persona = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(Persona.class);
		criteria.add(Property.forName("usuario").eq(usuario));
		criteria.add(Property.forName("password").eq(password));
		List<Persona> personas = this.getHibernateTemplate().findByCriteria(criteria);
		if ((personas != null) || (!personas.isEmpty())){
			persona = personas.get(0);
		}
		return persona;
	}

	@Override
	public void guardar(Asignatura asignatura) {
		this.getHibernateTemplate().saveOrUpdate(asignatura);
	}
	

}
